#include "key.h"
#include "stm32f1xx_hal.h"
#include "main.h"


/* 按键状态定义 ----------------------------------------------------------*/
#define KEY_PORT        GPIOA
#define KEY_PIN         KEY1_Pin          // 使用PA1引脚
#define KEY_LONG_TIME   800               // 长按时间阈值 800ms
#define KEY_DEBOUNCE    4                 // 消抖计数

/* 按键状态机状态 --------------------------------------------------------*/
typedef enum {
    KEY_IDLE = 0,
    KEY_PRESS_DEBOUNCE,
    KEY_SHORT_PRESS,
    KEY_LONG_PRESS,
    KEY_RELEASE_DEBOUNCE
} KeyStateTypeDef;

/* 按键状态结构体 --------------------------------------------------------*/
static struct {
    KeyStateTypeDef state;
    uint8_t debounce_cnt;
    uint16_t press_time;
    uint8_t short_event;
    uint8_t long_event;
} key_state = {0};

void Key_Init(void)
{
    // 清零按键状态结构体
    key_state.state = KEY_IDLE;
    key_state.debounce_cnt = 0;
    key_state.press_time = 0;
    key_state.short_event = 0;
    key_state.long_event = 0;
    
    // 确保GPIO已经初始化（MX_GPIO_Init()在main中已经调用）
}

/* 编码器增量缓存 --------------------------------------------------------*/
static int16_t encoder_last_value = 0;

/* 5ms周期扫描函数 - 放在SysTick或TIM中断中调用 ---------------------------*/
void KeyScan(void)
{
    uint8_t key_level = HAL_GPIO_ReadPin(KEY_PORT, KEY_PIN);
    
    switch(key_state.state) {
        case KEY_IDLE:
            if(key_level == GPIO_PIN_RESET) {  // 按键按下（低电平有效）
                key_state.state = KEY_PRESS_DEBOUNCE;
                key_state.debounce_cnt = 0;
            }
            break;
            
        case KEY_PRESS_DEBOUNCE:
            if(key_level == GPIO_PIN_RESET) {
                if(++key_state.debounce_cnt >= KEY_DEBOUNCE) {
                    key_state.state = KEY_SHORT_PRESS;
                    key_state.press_time = 0;
                }
            } else {
                key_state.state = KEY_IDLE;  // 抖动，回到空闲
            }
            break;
            
        case KEY_SHORT_PRESS:
            if(key_level == GPIO_PIN_RESET) {
                key_state.press_time += 5;  // 5ms周期
                if(key_state.press_time >= KEY_LONG_TIME) {
                    key_state.state = KEY_LONG_PRESS;
                    key_state.long_event = 1;  // 触发长按事件
                }
            } else {
                key_state.state = KEY_RELEASE_DEBOUNCE;
                key_state.debounce_cnt = 0;
            }
            break;
            
        case KEY_LONG_PRESS:
            if(key_level != GPIO_PIN_RESET) {
                key_state.state = KEY_RELEASE_DEBOUNCE;
                key_state.debounce_cnt = 0;
            }
            break;
            
        case KEY_RELEASE_DEBOUNCE:
            if(key_level != GPIO_PIN_RESET) {
                if(++key_state.debounce_cnt >= KEY_DEBOUNCE) {
                    if(key_state.press_time < KEY_LONG_TIME) {
                        key_state.short_event = 1;  // 触发短按事件
                    }
                    key_state.state = KEY_IDLE;
                }
            } else {
                key_state.state = KEY_SHORT_PRESS;  // 还是按下状态
            }
            break;
    }
}

/* 获取单击事件（进入/确认）----------------------------------------------*/
uint8_t Key_GetEvent_Enter(void)
{
    uint8_t ret = key_state.short_event;
    key_state.short_event = 0;
    return ret;
}

/* 获取长按事件（返回）--------------------------------------------------*/
uint8_t Key_GetEvent_Back(void)
{
    uint8_t ret = key_state.long_event;
    key_state.long_event = 0;
    return ret;
}

